/*
* $Id: ColumnDialog.cpp 117 2010-07-13 09:42:19Z volok-aleksej@yandex.ru $
*
* Copyright 2008 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ColumnDialog.h"
#include "Settings/Journal/ListBoxStateParser.h"

CColumnDialog::CColumnDialog(const std::tstring& strOperation)
: CDialog(IDD_NEW_COLUMN,
		  dynamic_cast<CWnd*>(IApplication::GetApp()->GetVisualManager()->GetVisualComponent(
		  IApplication::GetApp()->GetLanguageManager()->GetString(SETTINGS_ID))->GetComponentFrame())){
	m_strOperation = strOperation;
	m_bChange = FALSE;
}
CColumnDialog::~CColumnDialog(){}

void CColumnDialog::OnOK(){
	CString str;
	m_TypeCombo.GetWindowText(str);
	m_Column.s_type = (CUserFieldsTable::content::type)GetType(str.GetString());
	m_NameEdit.GetWindowText(str);
	m_Column.s_strName = str.GetString();
	m_bChange = TRUE;
	EndDialog(0);
}
BOOL CColumnDialog::OnInitDialog(){
	BOOL bRes = __super::OnInitDialog();
	std::tstring strCaption;
	if(m_strOperation == _ADD)
		strCaption = IApplication::GetApp()->GetLanguageManager()->GetString(ADD_ID);
	else if(m_strOperation == _CHANGE)
		strCaption = IApplication::GetApp()->GetLanguageManager()->GetString(CHANGE_ID);
	GetDlgItem(IDCANCEL)->SetWindowText(IApplication::GetApp()->GetLanguageManager()->GetString(CANCEL_ID).c_str());
	GetDlgItem(IDOK)->SetWindowText(strCaption.c_str());
	GetDlgItem(IDOK)->EnableWindow(FALSE);
	GetDlgItem(IDC_NAME)->SetWindowText(IApplication::GetApp()->GetLanguageManager()->GetString(NAME_ID).c_str());
	GetDlgItem(IDC_TYPE)->SetWindowText(IApplication::GetApp()->GetLanguageManager()->GetString(TYPE_ID).c_str());
	SetWindowText(strCaption.c_str());
	m_TypeCombo.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(STRING_ID).c_str());
	m_TypeCombo.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(INT_ID).c_str());
	m_TypeCombo.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(DOUBLE_ID).c_str());
	m_TypeCombo.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(BOOL_ID).c_str());
	m_TypeCombo.AddString(IApplication::GetApp()->GetLanguageManager()->GetString(DATE_ID).c_str());
	m_TypeCombo.SelectString(0, IApplication::GetApp()->GetLanguageManager()->GetString(STRING_ID).c_str());
	return bRes;
}

bool CColumnDialog::WindowCreate(){
	DoModal();
	return m_bChange;
}

void CColumnDialog::DoDataExchange(CDataExchange* pDX){
	__super::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TYPE_COMBO, m_TypeCombo);
	DDX_Control(pDX, IDC_NAME_EDIT, m_NameEdit);
}

CUserFieldsTable::content CColumnDialog::GetColumn(){
	return m_Column;
}

BEGIN_MESSAGE_MAP(CColumnDialog, CDialog)
	ON_EN_CHANGE(IDC_NAME_EDIT, OnEditChange)
END_MESSAGE_MAP()

void CColumnDialog::OnEditChange(){
	CString str;
	m_NameEdit.GetWindowText(str);
	if(str.GetLength() == 0)
		GetDlgItem(IDOK)->EnableWindow(FALSE);
	else
		GetDlgItem(IDOK)->EnableWindow(TRUE);
}

int CColumnDialog::GetType(const std::tstring& strType){
	if(strType == IApplication::GetApp()->GetLanguageManager()->GetString(STRING_ID))
		return CUserFieldsTable::content::STRING;
	else if(strType == IApplication::GetApp()->GetLanguageManager()->GetString(INT_ID))
		return CUserFieldsTable::content::INTEGER;
	else if(strType == IApplication::GetApp()->GetLanguageManager()->GetString(DOUBLE_ID))
		return CUserFieldsTable::content::DOUBLE;
	else if(strType == IApplication::GetApp()->GetLanguageManager()->GetString(BOOL_ID))
		return CUserFieldsTable::content::BOOL;
	else if(strType == IApplication::GetApp()->GetLanguageManager()->GetString(DATE_ID))
		return CUserFieldsTable::content::DATE;
	return CUserFieldsTable::content::STRING;
}